package com.feihong.txgw.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feihong.txgw.R;
import com.feihong.txgw.model.PRODUCT_TYPE;
import com.feihong.txgw.model.bean.home.ProductBaseModel;

import java.util.List;

/**
 * Created by Sabirjan on 2017/6/28.
 * TXGWApp.
 * 飞宏电子
 */

public class HomeGridViewAdapter extends BaseAdapter {

    private List<ProductBaseModel> datas;
    private LayoutInflater inflater;
    private int itemType = PRODUCT_TYPE.TYPE_PHONE;
    private Context mContext;

    public HomeGridViewAdapter(Context context, int type, List<ProductBaseModel> items) {
        this.inflater = LayoutInflater.from(context);
        this.datas = items;
        this.mContext = context;
        this.itemType = type;
    }

    @Override
    public int getCount() {
        if (datas == null) {
            return 0;
        }
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            switch (itemType) {
                case PRODUCT_TYPE.TYPE_PHONE_PARTS: // 配件
//                    convertView = inflater.inflate(R.layout.home_grid_phone_item, null);
//                    ViewHolder viewHolder = new ViewHolder();
//                    viewHolder.initViewNumber(convertView);
//                    convertView.setTag(viewHolder);
                    break;
                case PRODUCT_TYPE.TYPE_PHONE_NUM: // 号码精选
                    convertView = inflater.inflate(R.layout.home_grid_phone_num_item, null);
                    ViewHolder viewHolderNumber = new ViewHolder();
                    viewHolderNumber.initViewNumber(convertView);
                    convertView.setTag(viewHolderNumber);
                    break;
                case PRODUCT_TYPE.TYPE_PHONE_TARIFF: // 套餐优选
                    convertView = inflater.inflate(R.layout.home_grid_phone_traiff_item, null);
                    ViewHolder viewHolderPariff = new ViewHolder();
                    viewHolderPariff.initViewPackage(convertView);
                    convertView.setTag(viewHolderPariff);
                    break;
                default:
//                    convertView = inflater.inflate(R.layout.home_grid_phone_item, null);
                    break;
            }
        }
        initializeViews(datas.get(position), (ViewHolder) convertView.getTag());

        return convertView;
    }

    private void initializeViews(ProductBaseModel data, ViewHolder holder) {

        switch (itemType) {
            case PRODUCT_TYPE.TYPE_PHONE:
            case PRODUCT_TYPE.TYPE_PHONE_PARTS:
//                Glide.with(mContext).load(data.getPic()).asBitmap().placeholder(R.mipmap.ic_launcher).into(holder.imgHomeGridImage);

                break;
            case PRODUCT_TYPE.TYPE_PHONE_NUM:
                // Glide.with(mContext).load(data.getPic()).asBitmap().placeholder(R.mipmap.ic_launcher).into(holder.imgHomeGridImage);

                String number = data.getNumber();
                holder.mHomeNumberTv.setText(number);
                String adr = data.getRegion_name();
                String icpName = data.getIcp_name();
                StringBuffer stringBuffer = new StringBuffer();
                if(adr.length() > 4){
                    stringBuffer.append(adr.substring(0,4)).append("..");
                }else {
                    stringBuffer.append(adr);
                }
                if(icpName.length() > 2){
                    stringBuffer.append(icpName.substring(0,2));
                }else {
                    stringBuffer.append(icpName);
                }
                holder.mHomeNumberDes.setText(stringBuffer);
                break;
            case PRODUCT_TYPE.TYPE_PHONE_TARIFF:
                String title = data.getSpec_g_name();
                String tcDes = data.getTc_des();
                String tcSpec1 = data.getSpec_1();
                String tcSpec2 = data.getSpec_2();
                String tcSale = data.getSale();
                if(!TextUtils.isEmpty(tcSale) && !tcSale.equals("0")){
                    holder.mHomePackageOriginalPriceLayout.setVisibility(View.VISIBLE);
                    holder.mHomePackageDiscountOriginalPrice.setText("¥ "+data.getSale());
                }
                if (title.length() > 5) {
                    String newTitle = title.substring(0, 5);
                    holder.mHomePackageName.setText("【 " + newTitle + " 】..");
                } else {
                    holder.mHomePackageName.setText("【 " + title + " 】");
                }

                if(tcDes.length() > 8){
                    tcDes = tcDes.substring(0,8);
                }
                holder.mHomePackageDes.setText(tcDes);
                if(tcSpec1.length() > 6){
                    tcSpec1 = tcSpec1.substring(0,6);
                }
                holder.mHomePackageDiscountDetail.setText(tcSpec1);
                if(tcSpec2.length() > 6){
                    tcSpec2 = tcSpec2.substring(0,6);
                }
                holder.mHomePackageDiscountDes.setText(tcSpec2);
                holder.mHomePackageDiscountPresentPrice.setText("¥ "+data.getPrice());
//                holder.txt_home_grid_tcName.setText(data.getName());
                break;
            default:

        }
    }


    protected class ViewHolder {
        private TextView txtHomeGridTitle;
        private TextView txtHomeGridDes;
        //        private TextView txt_home_grid_tcName;
        private ImageView imgHomeGridImage;

        private TextView mHomeNumberTv, mHomeNumberDes;

        private TextView mHomePackageName, mHomePackageDes;
        // 套餐上面的详情 以及下面的介绍   套餐原价 套餐现价
        private TextView mHomePackageDiscountDes, mHomePackageDiscountDetail, mHomePackageDiscountOriginalPrice, mHomePackageDiscountPresentPrice;
        private RelativeLayout mHomePackageOriginalPriceLayout;
        private LinearLayout mLayout;
        public ViewHolder() {

        }

        public void initViewPackage(View convertView) {

//            txt_home_grid_tcName = (TextView) view.findViewById(R.id.txt_home_grid_tcName);
            mHomePackageName = (TextView) convertView.findViewById(R.id.home_package_name);
            mHomePackageDes = (TextView) convertView.findViewById(R.id.home_package_des);
            mHomePackageDiscountDes = (TextView) convertView.findViewById(R.id.home_package_discount_des);
            mHomePackageDiscountDetail = (TextView) convertView.findViewById(R.id.home_package_discount_detail);
            mHomePackageDiscountOriginalPrice = (TextView) convertView.findViewById(R.id.home_package_discount_original_price);
            mHomePackageDiscountPresentPrice = (TextView) convertView.findViewById(R.id.home_package_discount_precent_price);
            mHomePackageOriginalPriceLayout = (RelativeLayout) convertView.findViewById(R.id.home_package_discount_original_price_layout);
        }

        public void initViewNumber(View convertView) {
//            txt_home_grid_tcName = (TextView) view.findViewById(R.id.txt_home_grid_tcName);
//            imgHomeGridImage = (ImageView) convertView.findViewById(R.id.img_home_grid_image);


            mHomeNumberTv = (TextView) convertView.findViewById(R.id.txt_home_grid_title);
            mHomeNumberDes = (TextView) convertView.findViewById(R.id.txt_home_grid_des);
        }
    }

}
